package com.mdxl.layer_cj.controller;


import com.alibaba.fastjson.JSONObject;
import com.mdxl.layer_cj.shiro.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Controller
@RequestMapping(value = "/first")
public class ShiroLoginController {

    @PostMapping(value = "/doRegister")
    @ResponseBody
    public String doRegister(HttpServletRequest request,
                             HttpServletResponse response){
        JSONObject result = new JSONObject();
        String nickName   = ServletRequestUtils.getStringParameter(request,"nickName",null);
        result.put("nickName",nickName);
        result.put("account","76521");
        return result.toJSONString();
    }



    @RequestMapping(value="/login",method=RequestMethod.POST)
    public String login(@RequestParam("username") String username,
                        @RequestParam("password") String password,
                        @RequestParam("vcode") String vcode,
                        @RequestParam("rememberMe") Boolean rememberMe){

        System.out.println(username);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password,rememberMe);
        SecurityUtils.getSubject().login(token);

        return "redirect:/shiro/index";
    }

    @RequestMapping(value="/index",method= RequestMethod.GET)
    @ResponseBody
    public String home(){
        Subject subject = SecurityUtils.getSubject();
        User principal = (User)subject.getPrincipal();
        System.out.println(principal.getId());
        System.out.println(principal.getPassword());
        System.out.println(principal.getUsername());
        return "Home";
    }
}
